package com.gz.ezexcel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.fastjson.JSON;
import com.gz.ezexcel.filter.FreezeAndFilter;
import com.gz.ezexcel.service.DownloadExcelService;
import com.gz.ezexcel.vo.ExcelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author juice-gz
 * date 2022-01-06
 */
@RestController
@RequestMapping("/download")
public class DownloadExcelController  {
    @Autowired
    private DownloadExcelService downloadExcelService;

    /**
     * 文件下载并且失败的时候返回json（默认失败了会返回一个有部分数据的Excel）
     *
     * @since 2.1.1
     */

    @GetMapping("/filter")
    public void downloadFailedUsingJson(HttpServletResponse response) throws IOException {
        List<ExcelVO> excelVOList = createExcelVOData();
       downloadExcelService.downloadForFilter( response, excelVOList);
    }

    static List<ExcelVO> createExcelVOData() {
        List<ExcelVO> excelVOList =new ArrayList<ExcelVO>();
        for (int i = 0; i <10 ; i++) {
            ExcelVO vo = new ExcelVO();
            vo.setAge(i+1);
            vo.setDate(new Date());
            vo.setName("建哥"+i);
            excelVOList.add(vo);
        }
        return excelVOList;
    }
}
